package org.csu.agrisupply.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.agrisupply.api.common.CommonResponse;
import org.csu.agrisupply.api.entity.*;

import org.csu.agrisupply.api.persistence.FormMapper;
import org.csu.agrisupply.api.persistence.FormOfferMapper;
import org.csu.agrisupply.api.persistence.FormOrderMapper;
import org.csu.agrisupply.api.persistence.UserMapper;
import org.csu.agrisupply.api.service.FormOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("FormOrderService")
public class FormOrderServiceImpl implements FormOrderService {
    @Autowired
    private FormOrderMapper formOrderMapper;
    @Autowired
    private FormOfferMapper formOfferMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FormMapper formMapper;

    @Override
    public CommonResponse<List<FormOrder>> getFormOrderList() {
        List<FormOrder> formOrderList = formOrderMapper.selectList(null);
        if(formOrderList.isEmpty()){
            return CommonResponse.createForSuccessMessages("没有分类信息");
        }
        return CommonResponse.createForSuccess(formOrderList);
    }

    @Override
    public CommonResponse<FormOrder> getFormOrder(String orderId) {
        FormOrder formOrder = formOrderMapper.selectById(orderId);
        if(formOrder == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的formOrder");
        }
        return  CommonResponse.createForSuccess(formOrder);
    }

    @Override
    public CommonResponse<FormOrder> createFormOrder(String formId, String userId) {
        Form form = formMapper.selectById(formId);
        if(form == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的form");
        }
        else if(form.getStatus() == 1)
        {
            return CommonResponse.createForSuccessMessages("该form已完成");
        }
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        QueryWrapper<FormOffer> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("userId",userId);
        wrapper1.eq("formId",formId);
        FormOffer formOffer = formOfferMapper.selectOne(wrapper1);
        if(formOffer == null)
        {
            return CommonResponse.createForSuccessMessages("没有对应的formoffer");
        }
        //生成订单并插入表中
        FormOrder formOrder = new FormOrder();
        formOrder.setOrderId(getMax());
        formOrder.setCount(formOffer.getCount());
        formOrder.setPrice(formOffer.getPrice());
        formOrder.setBuyuserId(form.getUserId());
        formOrder.setSaleuserId(userId);
        formOrder.setFormId(formId);
        formOrder.setToAddress(form.getReceiptAddress());
        formOrder.setFromAddress(formOffer.getFromAddress());
        formOrderMapper.insert(formOrder);
        //删除对应formoffer
        QueryWrapper<FormOffer> wrapper2 = new QueryWrapper<FormOffer>();
        wrapper2.eq("userId",userId);
        wrapper2.eq("formId",formId);
        formOfferMapper.delete(wrapper2);
        //若需求量已经满足则订单状态改变，若需求量尚未满足则订单需求量改变
        if(form.getCount() > formOffer.getCount()){
            form.setCount(form.getCount() - formOffer.getCount());
            formMapper.updateById(form);
        }
        else {
            form.setStatus(1);
            formMapper.updateById(form);
        }
        return CommonResponse.createForSuccess("成功生成订单",formOrder);
    }
    public String getMax(){
        QueryWrapper<FormOrder> wrapper = new QueryWrapper<>();
        wrapper.select("orderId");
        List<Object> orderList = formOrderMapper.selectObjs(wrapper);
        int Max = 0;
        if(!orderList.isEmpty()){
            Max = Integer.parseInt(orderList.get(0).toString());
            for(Object item : orderList){
                int num = Integer.parseInt(item.toString());
                if (num >= Max)
                    Max = num;
            }
        }
        return Integer.toString(Max + 1);
    }
}
